/*
#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################
*/

/* user clicked set tag */

function setTags(resultnum) {
	/* show the add/save links */
	swapTagActions(resultnum);

	/* show the tag div */
	document.getElementById("tagcontainer" + resultnum).style.display = "block";
}

/* end */


/* user clicked save tags */

function saveTags(resultnum) {
	/* show the set link */
	swapTagActions(resultnum);

	/* hide the tag div */
	document.getElementById("tagcontainer" + resultnum).style.display = "none";
}

/* end */


/* user clicked add row*/

function addRow(resultnum,lasttag) {
	/* add row to list */
	addrow(resultnum,lasttag)
}

/* end */


/* swap tag actions for result */

function swapTagActions(resultnum) {
	/* get action div */
	actdiv = document.getElementById("tagactions" + resultnum);

	/* set tag */
	setcont = "<a class='fakelink' onclick='setTags("+resultnum+"); return false;'>Set Tags</a>";

	/* add/save tag */
	addsavecont = "<a class='fakelink' onclick='addrow("+resultnum+",3);'>Add Row</a> &nbsp&nbsp; <a class='fakelink' onclick='saveTags("+resultnum+"); return false;'>Save Tags</a>";

	/* check if set or add/save */
	setcheck = /Set Tags/;

	if (setcheck.test(actdiv.innerHTML)) {
		/* replace set with add/save */
		actdiv.innerHTML = addsavecont;
	}
	else {
		/* replace add/save with set */
		actdiv.innerHTML = setcont;
	}
}

/* end */


/* verify tag */

function verifyTag(inputbox) {
	/* get value */
	val = inputbox.value;

	/* if empty of only #, don't bother it */
	if (val == "") {
		return;
	}

	/* remove all but hex chars */
	val = val.replace(/[^0-9a-zA-Z-]/g,"");

	inputbox.value = val;
}

/* end */


/* add a row to the list */

function addrow(resultnum,lasttag) {
	/* quick check */
	if (lasttag > 40){
		alert("You have reached the tag limit.");
		return;
	}

	/* dummy div, end of the keyword boxes */
	endtag = document.getElementById("endtag"+resultnum);

	/* parent of the dummy */
	partag = endtag.parentNode;

	/* create the new row div */
	newtag = document.createElement("div");

	/* add new row to the fray */
	partag.insertBefore(newtag, endtag);

	/* fill the div with contents */
	newtag.innerHTML = '<div><input type="text" name="tag" size="20" maxlength="30" onkeyup="verifyTag(this);" value=""> ' +
			'<input type="text" name="tag" size="20" maxlength="30" onkeyup="verifyTag(this);" value=""> ' +
			'<input type="text" name="tag" size="20" maxlength="30" onkeyup="verifyTag(this);" value=""></div>';
}

/* end */


/* delete an object */

function delobj(id) {
	child = document.getElementById(id)
	par = child.parentNode;
	par.removeChild(child);
}

/* end */
